# coding: utf-8
from typing import Union
from enum import Enum

from PySide6.QtCore import QSize
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QToolButton


class TransparentToolButton(QToolButton):
    def __init__(self, icon: Union[QIcon, str, Enum] = None, parent=None):
        super().__init__(parent)
        self.setIcon(icon)
        self.setIconSize(QSize(16, 16))
        self.setMinimumSize(QSize(30, 30))
        self.setStyleSheet(
            '''
TransparentToolButton{
    background-color: transparent;
    border: none;
    border-radius: 4px;
    margin: 0;
}
TransparentToolButton:hover{
    background-color: rgba(255, 255, 255, 9);
    border: none;
}
TransparentToolButton:pressed {
    background-color: rgba(255, 255, 255, 6);
    border: none;
}
TransparentToolButton:disabled {
    background-color: transparent;
    border: none;
}
            '''
        )
        self._postInit()

    def _postInit(self):
        pass

    def setIcon(self, icon: Union[QIcon, str, Enum]):
        if icon is None:
            return
        if isinstance(icon, str):
            icon = QIcon(icon)
        elif isinstance(icon, Enum):
            icon = icon.icon()
        super().setIcon(icon)
